import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.app.common.utils.ContextHandlerUtil;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.wx.WeiXinUtil;
import com.bootdo.system.domain.TbWeixinconfig;
import com.bootdo.system.domain.TbWxuser;

public class cnmbbb2 {
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
    //拼接参数
    String parameter = 257+"";
    String key = UUID.randomUUID() + ".png";
    String res="/upLoad/"+key;
	  String accessToken =WeiXinUtil.getOauth2AccessToken2("wx798d61260ba95d82", "0a4268beb90365b03a1178ace409983f");

	  try{
	        URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
	        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
	        // conn.setConnectTimeout(10000);//连接超时 单位毫秒
	        // conn.setReadTimeout(2000);//读取超时 单位毫秒
	        // 发送POST请求必须设置如下两行
	        httpURLConnection.setDoOutput(true); // 打开写入属性
	        httpURLConnection.setDoInput(true); // 打开读取属性
	        httpURLConnection.setRequestMethod("POST");// 提交方式
	        // 不得不说一下这个提交方式转换！！真的坑。。改了好长时间！！一定要记得加响应头
	        httpURLConnection.setRequestProperty("Content-Type", "application/x-javascript; charset=UTF-8");// 设置响应头
	        // 获取URLConnection对象对应的输出流
	        PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
	        // 发送请求参数
	        JSONObject paramJson = new JSONObject();
	        paramJson.put("scene", parameter); // 你要放的内容
	        paramJson.put("path", "pages/index/index");
	        paramJson.put("width", 430); // 宽度
	        paramJson.put("auto_color", true);
	        printWriter.write(paramJson.toString());
	        // flush输出流的缓冲
	        printWriter.flush();
	        BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
	        OutputStream os = new FileOutputStream(new File("d:/var/uploaded_files/"+key));
	        System.out.println(key);
	        int len;
	        byte[] arr = new byte[1024];
	        while ((len = bis.read(arr)) != -1)
	        {
	            os.write(arr, 0, len);
	            os.flush();
	        }
	        os.close();
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }
    System.out.println(res);
	}
	/**
	 * 生成带参小程序二维码
	 * @param scene	要输入的内容
	 * @param accessToken	token
	 */
	public static void postMiniqrQr(String scene, String accessToken) {
	    try{
	    	String key = UUID.randomUUID() + ".png";
	        URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
	        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
	        // conn.setConnectTimeout(10000);//连接超时 单位毫秒
	        // conn.setReadTimeout(2000);//读取超时 单位毫秒
	        // 发送POST请求必须设置如下两行
	        httpURLConnection.setDoOutput(true); // 打开写入属性
	        httpURLConnection.setDoInput(true); // 打开读取属性
	        httpURLConnection.setRequestMethod("POST");// 提交方式
	        // 不得不说一下这个提交方式转换！！真的坑。。改了好长时间！！一定要记得加响应头
	        httpURLConnection.setRequestProperty("Content-Type", "application/x-javascript; charset=UTF-8");// 设置响应头
	        // 获取URLConnection对象对应的输出流
	        PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
	        // 发送请求参数
	        JSONObject paramJson = new JSONObject();
	        paramJson.put("scene", scene); // 你要放的内容
	        paramJson.put("path", "pages/index/index");
	        paramJson.put("width", 430); // 宽度
	        paramJson.put("auto_color", true);
	        printWriter.write(paramJson.toString());
	        // flush输出流的缓冲
	        printWriter.flush();
	        BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
	        OutputStream os = new FileOutputStream(new File("d:/var/uploaded_files/"+key));
	        System.out.println(key);
	        int len;
	        byte[] arr = new byte[1024];
	        while ((len = bis.read(arr)) != -1)
	        {
	            os.write(arr, 0, len);
	            os.flush();
	        }
	        os.close();
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }
	}

}
